﻿Namespace Web
    Partial Public Class Reserva

#Region "Dates"
        Public Property strCheckIn As String
            Get
                If CheckIn.HasValue Then
                    Return Right("00" & CheckIn.Value.Day, 2) & "/" & Right("00" & CheckIn.Value.Month, 2) & "/" & Right("0000" & CheckIn.Value.Year, 4)
                Else
                    Return Nothing
                End If
            End Get
            Set(value As String)
                Try
                    If value Is Nothing Then
                        CheckIn = Nothing
                    ElseIf Not IsDate(value) Then
                        CheckIn = Nothing
                    Else
                        CheckIn = Date.Parse(value)
                    End If
                    RaisePropertyChanged("strCheckIn")
                Catch ex As Exception
                    Throw New FormatException()
                End Try
            End Set
        End Property

        Public Property strCheckOut As String
            Get
                If CheckOut.HasValue Then
                    Return Right("00" & CheckOut.Value.Day, 2) & "/" & Right("00" & CheckOut.Value.Month, 2) & "/" & Right("0000" & CheckOut.Value.Year, 4)
                Else
                    Return Nothing
                End If
            End Get
            Set(value As String)
                Try
                    If value Is Nothing Then
                        CheckOut = Nothing
                    ElseIf Not IsDate(value) Then
                        CheckOut = Nothing
                    Else
                        CheckOut = Date.Parse(value)
                    End If

                    RaisePropertyChanged("strCheckOut")
                Catch ex As Exception
                    Throw New FormatException()
                End Try
            End Set
        End Property

        Public Property strDataNaixement As String
            Get
                If DataNaixement.HasValue Then
                    Return Right("00" & DataNaixement.Value.Day, 2) & "/" & Right("00" & DataNaixement.Value.Month, 2) & "/" & Right("0000" & DataNaixement.Value.Year, 4)
                Else
                    Return Nothing
                End If
            End Get
            Set(value As String)
                Try
                    If value Is Nothing Then
                        DataNaixement = Nothing
                    ElseIf Not IsDate(value) Then
                        DataNaixement = Nothing
                    Else
                        DataNaixement = Date.Parse(value)
                    End If

                    RaisePropertyChanged("strDataNaixement")
                Catch ex As Exception
                    Throw New FormatException()
                End Try
            End Set
        End Property

        Public Property strDataDocument As String
            Get
                If DataDocument.HasValue Then
                    Return Right("00" & DataDocument.Value.Day, 2) & "/" & Right("00" & DataDocument.Value.Month, 2) & "/" & Right("0000" & DataDocument.Value.Year, 4)
                Else
                    Return Nothing
                End If
            End Get
            Set(value As String)
                Try
                    If value Is Nothing Then
                        DataDocument = Nothing
                    ElseIf Not IsDate(value) Then
                        DataDocument = Nothing
                    Else
                        DataDocument = Date.Parse(value)
                    End If

                    RaisePropertyChanged("strDataDocument")
                Catch ex As Exception
                    Throw New FormatException()
                End Try
            End Set
        End Property
#End Region

#Region "Combos"

        Public Property TipusPagamentItem() As OpcioCombo
            Get
                Return OpcionsComboTipusPagaments.GetOpcions.Where(Function(f) f.Id = IdTipusPagament).FirstOrDefault()
            End Get
            Set(ByVal value As OpcioCombo)
                If value IsNot Nothing Then
                    Me.IdTipusPagament = value.Id
                    If Me.LLibreCaixa IsNot Nothing Then Me.LLibreCaixa.ActualitzarCampsCalculats()
                End If
            End Set
        End Property

        Public Property TipusSexeItem() As OpcioCombo
            Get
                If IdSexe.HasValue Then
                    Return OpcionsComboTipusSexes.GetOpcions.Where(Function(f) f.Id = IdSexe).FirstOrDefault()
                Else
                    Return Nothing
                End If
            End Get
            Set(ByVal value As OpcioCombo)
                If value IsNot Nothing Then
                    Me.IdSexe = value.Id
                End If
            End Set
        End Property

        Public Property TipusPaisItem() As OpcioCombo
            Get
                If IdPais.HasValue Then
                    Return OpcionsComboTipusPaisos.GetOpcions.Where(Function(f) f.Id = IdPais).FirstOrDefault()
                Else
                    Return Nothing
                End If

            End Get
            Set(ByVal value As OpcioCombo)
                If value IsNot Nothing Then
                    Me.IdPais = value.Id
                End If
            End Set
        End Property

        Public Property TipusDocumentItem() As OpcioCombo
            Get
                If IdTipusDocument.HasValue Then
                    Return OpcionsComboTipusDocuments.GetOpcions.Where(Function(f) f.Id = IdTipusDocument).FirstOrDefault()
                Else
                    Return Nothing
                End If
            End Get
            Set(ByVal value As OpcioCombo)
                If value IsNot Nothing Then
                    Me.IdTipusDocument = value.Id
                End If
            End Set
        End Property

#End Region

        Public Property strImport As String
            Get
                Return Format(Import, "0.00").Replace(".", ",")
            End Get
            Set(value As String)
                value = value.Replace(".", ",")
                Try
                    Import = Double.Parse(value)
                    RaisePropertyChanged("strImport")
                Catch ex As Exception
                    Throw New FormatException()
                End Try
            End Set
        End Property

        Private Sub OnImportChanged()
            RaisePropertyChanged("strImport")
            If Me.LLibreCaixa IsNot Nothing Then Me.LLibreCaixa.ActualitzarCampsCalculats()
        End Sub

        Private Sub OnCheckInChanged()
            RaisePropertyChanged("NombreNits")
        End Sub

        Private Sub OnCheckOutChanged()
            RaisePropertyChanged("NombreNits")
        End Sub
    End Class
End Namespace